package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.YxGoods;
import co.yixiang.modules.shop.domainExcel.YxGoodsExcel;
import co.yixiang.modules.shop.domainExcel.YxGoodsExcelData;
import co.yixiang.modules.shop.read.GoodsDataListener;
import co.yixiang.modules.shop.repository.YxGoodsRepository;
import co.yixiang.modules.shop.service.YxGoodsService;
import co.yixiang.modules.shop.service.dto.YxGoodsDto;
import co.yixiang.modules.shop.service.dto.YxGoodsQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxGoodsExcelDataMapper;
import co.yixiang.modules.shop.service.mapper.YxGoodsExcelMapper;
import co.yixiang.modules.shop.service.mapper.YxGoodsMapper;
import co.yixiang.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

/**
* @author frq
* @date 2020-08-24
*/
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxGoodsServiceImpl implements YxGoodsService {

    private final YxGoodsRepository yxGoodsRepository;

    private final YxGoodsMapper yxGoodsMapper;

    private final YxGoodsExcelMapper yxGoodsExcelMapper;

    private final GoodsDataListener goodsDataListener;

    private final YxGoodsExcelDataMapper yxGoodsExcelDataMapper;

    public YxGoodsServiceImpl(YxGoodsRepository yxGoodsRepository, YxGoodsMapper yxGoodsMapper, YxGoodsExcelMapper yxGoodsExcelMapper, GoodsDataListener goodsDataListener, YxGoodsExcelDataMapper yxGoodsExcelDataMapper) {
        this.yxGoodsRepository = yxGoodsRepository;
        this.yxGoodsMapper = yxGoodsMapper;
        this.yxGoodsExcelMapper = yxGoodsExcelMapper;
        this.goodsDataListener = goodsDataListener;
        this.yxGoodsExcelDataMapper = yxGoodsExcelDataMapper;
    }

    @Override
    public Map<String,Object> queryAll(YxGoodsQueryCriteria criteria, Pageable pageable){
        Page<YxGoods> page = yxGoodsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxGoodsMapper::toDto));
    }

    @Override
    public List<YxGoodsDto> queryAll(YxGoodsQueryCriteria criteria){
        return yxGoodsMapper.toDto(yxGoodsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public void download(List<YxGoodsDto> all, HttpServletResponse response) throws IOException {

        List<Map<String, Object>> list = new ArrayList<>();
        for (YxGoodsDto yxGoods : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("门店编号", yxGoods.getStoreId());
            map.put("类别编码", yxGoods.getCategoryCode());
            map.put("商品名称", yxGoods.getStoreName());
            map.put("供应商编码", yxGoods.getSupplierCode());
            map.put("unit", yxGoods.getUnit());
            map.put("起订量", yxGoods.getQuantity());
            map.put("商品规格名称", yxGoods.getSpecificatName());
            map.put("商品规格内容", yxGoods.getSpecificatContent());
            map.put("产品条码", yxGoods.getBarcode());
            map.put("进货价", yxGoods.getCost());
            map.put("成本价", yxGoods.getCostPrice());
            map.put("零售价", yxGoods.getRetailPrice());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);

    }

    /**
     * 百万级批量导入
     * @param inputStream
     */
    @Override
    public void doEasyExcelImportGoods(InputStream inputStream) {
        EasyExcel.read( inputStream, YxGoodsExcel.class, goodsDataListener).sheet().doRead();
    }

    @Override
    public List<YxGoodsExcelData> findListYxGoods() {
        List<YxGoods> list=yxGoodsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,null,criteriaBuilder));
        return yxGoodsExcelDataMapper.toDto(list);
    }

    /**
     * poi批量导入
     * @param list
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doPOIImportGoods(List<List<String>> list) {
        List<YxGoods> listGoods=new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (List<String> li : list) {
                YxGoods yxGoods=new YxGoods();
                yxGoods.setStoreId(li.get(0));
                yxGoods.setCategoryCode(li.get(1));
                yxGoods.setStoreName(li.get(2));
                yxGoods.setSupplierCode(li.get(3));
                yxGoods.setUnit(li.get(4));
                yxGoods.setQuantity(conInteger(li.get(5)));
                yxGoods.setSpecificatName(li.get(6));
                yxGoods.setSpecificatContent(li.get(7));
                yxGoods.setBarCode(li.get(8));
                yxGoods.setCost(conDecimal(li.get(9)));
                yxGoods.setCostPrice(conDecimal(li.get(10)));
                yxGoods.setRetailPrice(conDecimal(li.get(11)));
                yxGoods.setAddTime(new Date());
                listGoods.add(yxGoods);
                //满3000插入开始入库
                if(listGoods.size()==3000){
                    yxGoodsRepository.saveAll(listGoods);
                    listGoods.clear();
                }
            }
            //剩余的数据入库
            yxGoodsRepository.saveAll(listGoods);
        }
    }
    /**
     * 数值转换
     *
     * @param digital
     * @return
     */
    public BigDecimal conDecimal(String digital) {
        BigDecimal decimal;
        if (StringUtils.isNotEmpty(digital)) {
            decimal = new BigDecimal(digital);
        } else {
            decimal = BigDecimal.ZERO;
        }
        return decimal;
    }
    public Integer conInteger(String digital) {
        Integer number;
        if (StringUtils.isNotEmpty(digital)) {
            number = Integer.parseInt(digital);
        } else {
            number = 0;
        }
        return number;
    }

}